package br.com.registrofinancas.adapters;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import br.com.registrofinancas.R;
import br.com.registrofinancas.model.IncomeVO;

public class FinancasGanhosAdapter extends BaseAdapter {

	private List<IncomeVO> incomesVOS;
	private LayoutInflater inflater;
		
	public FinancasGanhosAdapter(Context context, List<IncomeVO> incomesVOS) {
		this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.incomesVOS = incomesVOS;
	}

	@Override
	public int getCount() {
		return incomesVOS.size();
	}

	@Override
	public Object getItem(int position) {
		return incomesVOS.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		ViewHolderItem viewHolderItem;
		
		IncomeVO icIncomeVO = incomesVOS.get(position);
		
		if (convertView == null) {			
			convertView = inflater.inflate(R.layout.activity_financas_item_ganhos, parent, false);
		
			viewHolderItem = new ViewHolderItem();
			
			viewHolderItem.tvCategoria = (TextView) convertView.findViewById(R.id.activity_financas_item_ganhos_tv_categoria);
			
			viewHolderItem.tvData = (TextView) convertView.findViewById(R.id.activity_financas_item_ganhos_tv_data);
			
			viewHolderItem.tvValor = (TextView) convertView.findViewById(R.id.activity_financas_item_ganhos_tv_valor);
			
			viewHolderItem.tvSaldo = (TextView) convertView.findViewById(R.id.activity_financas_item_ganhos_tv_saldo);
						
			convertView.setTag(viewHolderItem);
			
		}else{
			viewHolderItem = (ViewHolderItem) convertView.getTag();
		}
		
		viewHolderItem.tvCategoria.setText(icIncomeVO.getCategory().getName());
		viewHolderItem.tvData.setText(formater(icIncomeVO.getOperationDate()));
		viewHolderItem.tvValor.setText(String.valueOf(icIncomeVO.getValue()));
		viewHolderItem.tvSaldo.setText(String.valueOf(icIncomeVO.getSaldo()));
		
		return convertView;
	}
	
	public String formater(Date date){
		SimpleDateFormat  formater = new SimpleDateFormat("EEE - dd/MM/yy");
		return formater.format(date);
	}

	private static class ViewHolderItem {
		TextView tvCategoria, tvData, tvValor, tvSaldo;
	}
}
